package com.css.fxfzysx.modules.ysxManagement.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.ysxManagement.entity.DiBoundEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.DynamicFieldsEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.DiBoundRepository;
import com.css.fxfzysx.modules.ysxManagement.repository.ZqtCiRepository;
import com.css.fxfzysx.modules.ysxManagement.service.DiBoundService;
import com.css.fxfzysx.modules.ysxManagement.service.ZqtCiService;
import com.css.fxfzysx.modules.ysxManagement.vo.DiBoundVo;
import com.css.fxfzysx.util.PlatformDateUtils;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.util.PlatformSessionUtils;
import com.css.fxfzysx.util.UUIDGenerator;
import com.css.fxfzysx.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DiBoundServiceImpl implements DiBoundService {

    @Autowired
    private DiBoundRepository diBoundRepository;

    @Autowired
    private ZqtCiService zqtCiService;

    @Autowired
    private ZqtCiRepository zqtCiRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse saveOrUpdateDiBound(DiBoundVo diBoundVo) {
        try {
            List<DiBoundEntity> diBoundList = diBoundVo.getDiBoundList();
            List<DynamicFieldsEntity> dynamicFieldsList = diBoundVo.getDynamicFieldsList();
            if (PlatformObjectUtils.isNotEmpty(diBoundList)) {
                diBoundList.forEach(it -> {
                    if(PlatformObjectUtils.isEmpty(it.getCdibId())){
                        it.setCdibId(UUIDGenerator.getUUID());
                        it.setCreateUser(PlatformSessionContext.getUserID());
                        it.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                        it.setDelFlag("0");
                        it.setDefaultData("1");
                        diBoundRepository.saveDiBound(it);
                    }else {
                        it.setUpdateUser(PlatformSessionUtils.getUserId());
                        it.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                        diBoundRepository.updateDiBound(it);
                    }
                });
            }
            zqtCiService.saveOrUpdateDynamicFields(dynamicFieldsList);
            return RestResponse.succeed("成功!");
        } catch (Exception e) {
            String errorMessage = "失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse getAllDiBound(String titleName) {
        try {
            DiBoundVo diBoundVo = new DiBoundVo();
            List<DiBoundEntity> diBoundList = diBoundRepository.getAllDiBound();
            List<DynamicFieldsEntity> dynamicFields = zqtCiRepository.getDynamicFields(titleName);
            diBoundVo.setDiBoundList(diBoundList);
            diBoundVo.setDynamicFieldsList(dynamicFields);
            return RestResponse.succeed(diBoundVo);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse deleteDiBoundById(String diBoundIds,String arrangeIds) {
        try {
            if(PlatformObjectUtils.isNotEmpty(diBoundIds)){
                for (String diBoundId : diBoundIds.split(",")) {
                    diBoundRepository.deleteDiBoundById(diBoundId);
                }
            }
            if(PlatformObjectUtils.isNotEmpty(arrangeIds)){
                for (String arrangeId : arrangeIds.split(",")) {
                    zqtCiRepository.deleteArrangeById(arrangeId);
                    diBoundRepository.deleteDiBoundByProjectId(arrangeId);
                }
            }
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }
}
